﻿using System;
using Cysharp.Threading.Tasks;
using UnityEngine;
using UnityEngine.UI;
using WssUtils;

namespace JQMergeFramework
{
    public class UIAchievementFinishTips : MonoBehaviour
    {
        public Text buildNumTxt;
        public Text achievementName;
        public Image buildingImg;
        
        private CityTargetCfg mCfg;

        private bool isClose = false;

        private CoroutineQueue.CheckCommand mCommand;
        private void Awake()
        {
            UIManager.Instance.AddBtnListener(this.transform);
        }

        public void Open(CoroutineQueue.CheckCommand command)
        {
            mCommand = command;
            
            GetComponent<UIPopAnimate>().PopStart();
            
            mCfg = TableConfig.Instance.GetFinishedTargetCfg(DataManager.Instance.AchievementFinishIndex);
            isClose = false;

            CommonFun.DelayCallback(() =>
            {
                isClose = true;
            }).Forget();
            RefreshUI();
        }

        void RefreshUI()
        {
            achievementName.text = mCfg.name;
            buildNumTxt.text = string.Format("盖楼{0}层", DataManager.Instance.BuildCityIdList.Count);
            buildingImg.sprite = MergeManager.Instance.scriptablePrefab.GetBuildingSprite(mCfg.imgName);
            buildingImg.SetNativeSize();
        }
        
        void OnBtnClick(Transform trans)
        {
            AudioManager.Instance.Play(AudioName.Click);
            
            switch (trans.name)
            {
                case "BtnUI":
                    Close();
                    break;
            }
        }

        void Close()
        {
            if (isClose)
            {
                GetComponent<UIPopAnimate>().PopEnd();
                mCommand?.Complete();
                //展示获得成就飘字
                UIManager.Instance.GetWindow<UIGame>().ShowAchievementTag(mCfg);
            }
        }

    }
}